package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.dto.AssetsStateDTO;
import com.key.entity.AssetType;
import com.key.mapper.AssetTypeMapper;
import com.key.mapper.AssetsMapper;
import com.key.service.AssetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AssetTypeServiceImpl implements AssetTypeService {

    @Autowired
    private AssetTypeMapper assetTypeMapper;
    @Autowired
    private AssetsMapper assetsMapper;
    @Override
    public R SelectAllType() {
        return null;
    }

    @Override
    public R findAllAssetsType() {

        R r=new R();
        List<AssetType> assetTypes = assetTypeMapper.findAllAssetType();
        if (assetTypes.size()>0){
            r=R.ok();
            r.setData(assetTypes);
            return r;
        }
        r=R.no();
        return r;
    }

    @Override
    @Transactional
    public R addAssetType(AssetType assetType) {
        R r=new R();
        AssetType assetType1 = assetTypeMapper.findAssetTypeByAssetTypeNum(assetType.getAssetTypeNum());
        try {
            if (assetType1==null){
                int i = assetTypeMapper.insertAssetType(assetType);
                AssetType assetType2 = assetTypeMapper.findAssetTypeByAssetTypeNum(assetType.getAssetTypeNum());
                for (int j = 1; j <5; j++) {
                    Map map=new HashMap();
                    map.put("assetTypeId",assetType2.getId());
                    map.put("state",j);
                    assetTypeMapper.insertAssetTypeBusiness(map);
                }
                assetType2.getId();
                return i>0?R.ok():R.no();
            }else {
                r.setCode(200);
                r.setMsg("已存在");
            }
            return r;
        }catch (Exception e){
            throw new TranException("添加失败");
        }

    }

    @Override
    public R findAssetsType(Integer assetTypeId) {
        R r=new R();
        AssetType assetType = assetTypeMapper.findAssetTypeById(assetTypeId);
        if (assetType!= null){
            r=R.ok();
            r.setData(assetType);
            return r;
        }
        r=R.no();
        return r;
    }

    @Override
    public R updateAssetType(AssetType assetType) {
        int i = assetTypeMapper.updateAssetType(assetType);
        return i>0?R.ok():R.no();
    }

    @Override
    public R deleteAssetTypes(List<Integer> assetTypeIds) {
        int i = assetTypeMapper.deleteAssetTypes(assetTypeIds);
        return i>0?R.ok():R.no();
    }

    @Override
    public R disableAssetType(Integer assetTypeId) {
        int i = assetTypeMapper.updateAssetTypeStateById(assetTypeId);
        return i>0?R.ok():R.no();
    }

    @Override
    public R enableAssetType(Integer assetTypeId) {
        int i = assetTypeMapper.changeAssetTypeStateById(assetTypeId);
        return i>0?R.ok():R.no();
    }

    @Override
    public R findAssetState() {
        Integer allCount=0;
        List<Integer> list=new ArrayList();
        for (int i = 1; i <10 ; i++) {
            int count = assetsMapper.findAssetsState(i);
            list.add(count);
            allCount+=count;
        }
        AssetsStateDTO assetsStateDTO=new AssetsStateDTO(allCount,list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5),list.get(6),list.get(7),list.get(8));
        R r=R.ok();
        r.setData(assetsStateDTO);
        return r;
    }

    @Override
    public R findAssetTypeUse() {
        List<AssetType> allAssetType = assetTypeMapper.findAllAssetType();
        List<AssetsStateDTO> assetsStateDTOS=new ArrayList<>();
        for (AssetType assetType : allAssetType) {
            Integer allCount=0;
            List<Integer> list=new ArrayList();
            Integer assetTypeNum = assetType.getAssetTypeNum();
            for (int i = 1; i <10 ; i++) {
                Map map=new HashMap<>();
                map.put("assetTypeNum",assetTypeNum);
                map.put("state",i);
                int count = assetsMapper.findAssetsTypeUse(map);
                list.add(count);
                allCount+=count;
            }
            AssetsStateDTO assetsStateDTO=new AssetsStateDTO(assetType.getAssetTypeNum(),assetType.getAssetTypeName(),allCount,list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5),list.get(6),list.get(7),list.get(8));
            assetsStateDTOS.add(assetsStateDTO);
        }
        R r=R.ok();
        r.setData(assetsStateDTOS);
        return r;
    }

    @Override
    public R findAssetTypeUseByAssetTypeId(Integer id) {
        AssetType assetType = assetTypeMapper.findAssetTypeById(id);
        Integer allCount=0;
            List<Integer> list=new ArrayList();
            for (int i = 1; i <10 ; i++) {
                Map map=new HashMap<>();
                map.put("assetTypeId",id);
                map.put("state",i);
                int count = assetsMapper.findAssetsTypeUseById(map);
                list.add(count);
                allCount+=count;
            }
            AssetsStateDTO assetsStateDTO=new AssetsStateDTO(assetType.getAssetTypeNum(),assetType.getAssetTypeName(),allCount,list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5),list.get(6),list.get(7),list.get(8));
            List<AssetsStateDTO> assetsStateDTOS=new ArrayList<>();
            assetsStateDTOS.add(assetsStateDTO);
            R r=R.ok();
            r.setData(assetsStateDTOS);
            return r;
    }
}
